<?php
	/**
	 * Elgg category - delete action
	 * 
	 * @package Elgg SocialCommerce
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Cubet Technologies
	 * @copyright Cubet Technologies 2009-2010
	 * @link http://elgghub.com/
	 **/
		global $CONFIG;
		$guid = (int) get_input('stores');
		$path = (int) get_input('path');
		
		
		if ($product = get_entity($guid)) {

			if ($product->canEdit()) {

				$container = get_entity($product->container_guid);
				
				$images = unserialize($product->images);
				
				foreach ($images as $image){
					if($image['path']!=$path){
						$new_images[] = $image;
					}
				}
				
				$product->images = serialize($new_images);
				if(!$product->save()){
					register_error(elgg_echo("image:deletefailed"));
				}
				else {
					//delete the image
					delete_images($guid, $path, $CONFIG->pluginname, $product->owner_guid);

					system_message(elgg_echo("image:deleted"));
				}
				
				

			} else {
				
				$container = $_SESSION['user'];
				register_error(elgg_echo("image:deletefailed"));
				
			}

		} else {
			
			register_error(elgg_echo("image:deletefailed"));
			
		}
		
		forward($CONFIG->url."mod/".$CONFIG->pluginname."/edit.php?stores_guid=".$guid);

?>